﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqLiabilities
    {
        
        public List<Liability> GetLiabilities(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<LiabilityEntity> query = db.LiabilityEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "LiabilityID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.LiabilityID);
                            else
                                query = query.OrderByDescending(c => c.LiabilityID);
                            break;

                    }
                }
                return query.Select(c => Mapper.ToLiabilityObject(c)).ToList();
            }
        }


        public Liability GetLiability(int liabilityId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToLiabilityObject(db.LiabilityEntities
                            .SingleOrDefault(p => p.LiabilityID == liabilityId));
            }
        }




        //public List<Liability> GetLiabilitiesForCustomer(string customerId)
        //{
        //    using (SODataDataContext db = DataContextFactory.CreateContext())
        //    {
        //        Income inco = new Income();

        //        IQueryable<LiabilityEntity> liability = from lia in db.LiabilityEntities
        //                                                where lia.CustomerID == customerId
        //                                                select lia;


        //        return liability.Select(c => Mapper.ToLiabilityObject(c)).ToList();

        //    }

        //}



        public Liability GetLiabilitiesForCustomer(string customerId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                Liability li = new Liability();

                var liability = (from lia in db.LiabilityEntities
                                 where lia.CustomerID == customerId
                                 group lia by new { lia.CustomerID } into g
                                 select new Liability 
                                 {
                                   CustomerId = g.FirstOrDefault().CustomerID,
                                   LiabilityId = g.FirstOrDefault().LiabilityID,
                                   UnProLoans = (float)g.Sum(p => p.UnProLoans).GetValueOrDefault(),
                                   Others = (float)g.Sum(p => p.Others).GetValueOrDefault()
                                 });



                if (liability.FirstOrDefault() != null)
                {
                    li.CustomerId = liability.FirstOrDefault().CustomerId;
                    li.LiabilityId = liability.FirstOrDefault().LiabilityId;
                    li.UnProLoans = liability.FirstOrDefault().UnProLoans;
                    li.Others = liability.FirstOrDefault().Others;

                }
                else
                {
                    li.LiabilityId = 0;
                    li.UnProLoans = 0;
                    li.Others = 0;
                }

                return li;

                

            }

            //using (SODataDataContext db = DataContextFactory.CreateContext())
            //{
            //    Income inco = new Income();

            //    IQueryable<LiabilityEntity> liability = from lia in db.LiabilityEntities
            //                                            where lia.CustomerID == customerId
            //                                            select lia;


            //    return liability.Select(c => Mapper.ToLiabilityObject(c)).ToList();

            //}
        }



        public void InsertLiability(Liability liability)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    LiabilityEntity entity = Mapper.ToLiabilityEntity(liability);
                    db.LiabilityEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    liability.LiabilityId = entity.LiabilityID;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to insert liability record was made before your changes.");
                }
                catch (Exception)
                {
                    throw new Exception("Unable to Insert Liability " + liability.LiabilityId.ToString());
                }
            }
        }

        public int UpdateLiability(Liability liability)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    LiabilityEntity entity = Mapper.ToLiabilityEntity(liability);
                    db.LiabilityEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update liabilities record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public int DeleteLiability(Liability liability)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    LiabilityEntity entity = Mapper.ToLiabilityEntity(liability);
                    db.LiabilityEntities.Attach(entity, false);
                    db.LiabilityEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to delete liability was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    }
}
